﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BookStore.Platform.objects;
using BookStore.Platform.data;
using System.IO;

namespace BookStore.Platform.apis
{
    public interface IBookApi
    {
        void Save(Book book);

        Book GetByName(string bookName, Guid webSiteId);

        Book GetById(Guid bookId);
    }

    public class BookApi:IBookApi
    {
        private IConvertApi convertApi = null;
        private IWebSiteApi webSiteApi = null;
        public BookApi(IConvertApi convertApi, IWebSiteApi webSiteApi)
        {
            this.convertApi = convertApi;
            this.webSiteApi = webSiteApi;
        }
        #region IBookApi Members

        public void Save(objects.Book book)
        {
            using (BookStoreDataContext context = new BookStoreDataContext()) 
            {
                var entity = context.t_books.FirstOrDefault(x => x.BookName.Equals(book.BookName) 
                    && x.WebsiteId == book.WebSiteId);
                if (entity == null)
                    context.t_books.InsertOnSubmit(convertApi.convertBizToEntity(book));
                else
                {
                    
                    entity.LastChapter = book.LastChapter;
                    entity.UpdatedOn = DateTime.Now;
                    entity.isDownloadCompleted = book.isDownloadCompleted;
                    entity.BookStatus = book.BookStatus;
                    entity.BookSummary = book.BookSummary;
                    entity.BookImage = book.BookImage;
                   
                }
                try
                {
                    context.SubmitChanges();

                    entity = context.t_books.FirstOrDefault(x => x.BookName.Equals(book.BookName)
                        && x.WebsiteId == book.WebSiteId);
                }
                catch (Exception e)
                {
                    throw e;
                }
                //check book folder exist or not
                //get website folder
                WebSite webSite = webSiteApi.GetById(entity.WebsiteId);
                if (webSite == null)
                    throw new Exception("parent website doesn't exist");
                string websiteFolder = webSite.WebSiteName;
                if (Directory.Exists(websiteFolder))
                {
                    string bookFolder = websiteFolder + "\\" + book.BookName;
                    if (!Directory.Exists(bookFolder))
                    {
                        try
                        {
                            Directory.CreateDirectory(bookFolder);
                            entity.Folder = bookFolder;
                            context.SubmitChanges();
                            book.Folder = entity.Folder;
                            book.BookId = entity.BookId;
                        }
                        catch (Exception)
                        {
                            throw;
                        }
                    }                    
                }
                else
                {
                    throw new Exception("WebSite directory doesn't exist");
                }
            }
        }
        public Book GetById(Guid bookId) 
        {
            using (BookStoreDataContext context = new BookStoreDataContext())
            {
                var entity = context.t_books.FirstOrDefault(x => x.BookId == bookId);
                if (entity == null)
                    return null;
                else
                    return convertApi.convertEntityToBiz(entity);
            }
        }
        public Book GetByName(string bookName, Guid webSiteId)
        {
            using (BookStoreDataContext context = new BookStoreDataContext())
            {
                var entity = context.t_books.FirstOrDefault(x => x.BookName.Equals(bookName) && x.WebsiteId == webSiteId);
                if (entity == null)
                    return null;
                else
                    return convertApi.convertEntityToBiz(entity);
            }
        }
        #endregion

        #region
        //private Book convertEntityToBiz(t_book entity)
        //{
        //    Book book = new Book() 
        //    {
        //        BookAuthor = entity.BookAuthor,
        //        BookCategory = entity.BookCategory,
        //        BookId = (entity.BookId != null) ? entity.BookId : Guid.Empty,
        //        BookImage = entity.BookImage,
        //        BookName = entity.BookName,
        //        BookStatus = entity.BookStatus,
        //        BookSummary = entity.BookSummary,
        //        BookUrl = entity.BookUrl,               
        //        Folder = entity.Folder,
        //        LastChapter = entity.LastChapter,
        //        WebSiteId = entity.WebsiteId,
                
        //    }
        //    ;

        //    return book;
        //}
        //private t_book convertBizToEntity(Book book)
        //{
        //    t_book entity = new t_book() 
        //    {
        //        BookAuthor = book.BookAuthor,
        //        BookCategory = book.BookCategory,
        //        BookId = (book.BookId != null)? book.BookId : Guid.Empty  ,
        //        BookImage = book.BookImage,
        //        BookName = book.BookName,
        //        BookStatus = book.BookStatus,
        //        BookSummary = book.BookSummary,
        //        BookUrl = book.BookUrl,
        //        CreatedOn = DateTime.Now,
        //        Folder = book.Folder,
        //        LastChapter = book.LastChapter,
        //        WebsiteId = book.WebSiteId,
        //        UpdatedOn = DateTime.Now,
                
        //    };
        //    return entity;
        //}
        #endregion
    }
}
